package com.example.ymm.modules1.service.dd;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiAttendanceGetattcolumnsRequest;
import com.dingtalk.api.request.OapiAttendanceGetcolumnvalRequest;
import com.dingtalk.api.request.OapiAttendanceGetupdatedataRequest;
import com.dingtalk.api.response.OapiAttendanceGetattcolumnsResponse;
import com.dingtalk.api.response.OapiAttendanceGetcolumnvalResponse;
import com.dingtalk.api.response.OapiAttendanceGetupdatedataResponse;
import com.example.ymm.exception.YmException;
import com.example.ymm.modules1.service.dd.ms.AccessTokenH5Service2;
import com.taobao.api.ApiException;
import com.taobao.api.internal.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class DingdingCheckService {

    /**
     * 员工考勤统计
     */

    /**
     * 钉钉部门信息
     */
    @Autowired
    AccessTokenH5Service2 accessTokenH5Service;


    /**
     * 获取员工某一天的考勤记录
     * @param userId
     * @param work_date
     * @return
     */
    public JSONObject getUserAttendance(String userId, Date work_date){
        JSONObject json=null;
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getupdatedata");
        OapiAttendanceGetupdatedataRequest req = new OapiAttendanceGetupdatedataRequest();
        req.setUserid(userId);
        req.setWorkDate(work_date);
        String accessToken = accessTokenH5Service.getAccessToken();

        OapiAttendanceGetupdatedataResponse rsp = null;
        try {
            rsp = client.execute(req, accessToken);
            JSONObject result=JSONObject.parseObject(rsp.getBody());

            if("0".equals(result.getString("errcode"))){
                json=JSONObject.parseObject(result.getString("result"));
            }else{
                throw new YmException(result.getString("errmsg"));
            }
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
        return json;
    }


    /**
     * 获取考勤 列定义
     */
    public JSONObject getattcolumns(){
        JSONObject json=null;
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getattcolumns");
        OapiAttendanceGetattcolumnsRequest req = new OapiAttendanceGetattcolumnsRequest();
        String accessToken = accessTokenH5Service.getAccessToken();

        OapiAttendanceGetattcolumnsResponse rsp = null;
        try {
            rsp = client.execute(req, accessToken);
            JSONObject result=JSONObject.parseObject(rsp.getBody());
            if("0".equals(result.getString("errcode"))){
                json=JSONObject.parseObject(result.getString("result"));
            }else{
                throw new YmException(result.getString("errmsg"));
            }
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
        return json;
    }

    /**
     * 获取考勤列值
     */
    public JSONObject getcolumnval(String userId){
        JSONObject json=null;
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getcolumnval");
        OapiAttendanceGetcolumnvalRequest req = new OapiAttendanceGetcolumnvalRequest();
        String accessToken = accessTokenH5Service.getAccessToken();

        req.setUserid(userId);
        /**
         * 470928322 工作日加班
         * 470928323 休息日加班
         * 470928324 节假日加班
         * 846290067 加班总时长
         * 470928333 下班2打卡时间
         */
        req.setColumnIdList("470928322,470928323,470928324,846290067,470928333");
        req.setFromDate(StringUtils.parseDateTime("2025-06-01 00:00:00"));
        req.setToDate(StringUtils.parseDateTime("2025-06-30 23:59:59"));
        try {
            OapiAttendanceGetcolumnvalResponse rsp = client.execute(req, accessToken);
            JSONObject result=JSONObject.parseObject(rsp.getBody());
            if("0".equals(result.getString("errcode"))){
                json=JSONObject.parseObject(result.getString("result"));
            }else{
                throw new YmException(result.getString("errmsg"));
            }
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
        return json;

    }

}
